package synchronized_;


/*
*实现：线程安全的方法
*
* 方式二：同步方法
* 将synchronized写到方法上
*
* */
public class MySynchronizedDemo1  implements Runnable {
    private  int ticket = 100;

    @Override
    public void run() {
        while (true){
            if (ticket>0){
                show();
            }else {
                break;
            }

        }
    }

    private  synchronized void show() {
        if (ticket>0){
            System.out.println(Thread.currentThread().getName()+"出票："+ticket);
            ticket--;
        }
    }
}

class TestThreadDemo2{
    public static void main(String[] args) {

        MySynchronizedDemo1 threadDemo1 = new MySynchronizedDemo1();

        Thread thread  = new Thread(threadDemo1);
        Thread thread1  = new Thread(threadDemo1);
        Thread thread2 = new Thread(threadDemo1);
        thread.setName("窗口一");
        thread1.setName("窗口二");
        thread2.setName("窗口三");
        thread.start();
        thread1.start();
        thread2.start();
    }
}
